package com.baigt.j8.lambda;

import java.util.function.Consumer;
import java.util.function.Supplier;

public class LambdaScopeTest {
    public int x = 0;

    class FirstLevel {

        public int x = 1;

        void methodInFirstLevel(int x) {

            // The following statement causes the compiler to generate
            // the error "local variables referenced from a lambda expression
            // must be final or effectively final" in statement A:
            //
            // x = 99;

            Consumer<Integer> myConsumer = (y) ->
            {
                System.out.println("x = " + x); // Statement A
                System.out.println("y = " + y);
                System.out.println("this.x = " + this.x);
                System.out.println("LambdaScopeTest.this.x = " +
                        LambdaScopeTest.this.x);
            };

            myConsumer.accept(x);

        }
    }

    public static void main(String... args) {
        LambdaScopeTest st = new LambdaScopeTest();
        LambdaScopeTest.FirstLevel fl = st.new FirstLevel();
        fl.methodInFirstLevel(23);

        Runnable hello = () -> System.out.println("hello");
        Supplier<String> result = () -> "1";
        Supplier<String> result2 = () -> {return "1";};
        Consumer<String> tConsumer = (String r) -> System.out.println(r);
        Consumer<String> tConsumer2 = (String r) -> {System.out.println(r);};
    }
}
